/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package android.isisufes.home2;

import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.isisufes.util.DBHelper;
import android.util.Log;

public class Commu {
	private String desc, name, owner;

	long id;

	Vector<Commu> communities = new Vector<Commu>();

	public static Commu[] fromDataBase() {
		return null;
	} // TODO if needed

	public Vector<Commu> fromXml(InputSource xml) {

		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp;
			sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			MyHandler handler = new Commu.MyHandler();
			xr.setContentHandler(handler);
			xr.setErrorHandler(handler);
			xr.parse(xml);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}

		return communities;
	}

	class MyHandler extends DefaultHandler {
		String lastElement;
		boolean begin;
		boolean valid;
		int count = 0;

		public void endElement(String uri, String localName, String qName) {
			begin = false;
		}

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) {
			begin = true;

			if (localName.compareTo("Community") == 0) {
				count++;
				communities.add(new Commu());
				valid = true;
			}
			lastElement = localName;
			for (int i = 0; i < attributes.getLength(); i++) {
				lastElement = attributes.getValue(i);
				// Log.d("DEBUG","*******"+lastElement);
			}
		}

		public void characters(char[] ch, int start, int length) {
			if (valid) {
				String s = new String(ch, start, length);
				if (lastElement == null)
					return;
				if (!begin)
					return;
				if (lastElement.compareTo("idCommunity") == 0) {
					if (s.compareTo("null") != 0) {
						// Log.d("DEBUG","id:"+s);
						Commu commu = communities.get(count - 1);
						commu.id = Integer.parseInt(s);
					}
				} else if (lastElement.compareTo("name") == 0) {
					if (s.compareTo("null") != 0) {
						// Log.d("DEBUG","name:"+s);
						Commu commu = communities.get(count - 1);
						commu.name = s;
					}
				} else if (lastElement.compareTo("description") == 0) {
					if (s.compareTo("null") != 0) {
						// Log.d("DEBUG","description:"+s);
						Commu commu = communities.get(count - 1);
						commu.desc = s;
					}
				} else if (lastElement.compareTo("owner") == 0) {
					if (s.compareTo("null") != 0) {
						// Log.d("DEBUG","description:"+s);
						Commu commu = communities.get(count - 1);
						commu.owner = s;
					}
				}
			}
		}
	}

	public String toXml() {
		return null;
	}// TODO

	public boolean toDataBase(SQLiteDatabase db, String user) {
		try {
			DBHelper toRecord = new DBHelper();
			toRecord.onCreate(db);
			Cursor query = db.query("communities", null, "user='" + user
					+ "' and id=" + id, null, null, null, null);
			if (query.getCount() == 0) {
				db.execSQL("INSERT INTO communities (id, name, owner, desc, user) values("
						+ id + ", '"
						+ name + "', '"
						+ owner + "', '"
						+ desc + "','"
						+ user + "');");
			}
			query.close();
			return true;
		} catch (SQLException e) {
			Log.d("DEBUG", "db 4 error");
			return false;
		}
	}

	public String getOwnerName() {
		return owner;
	}

	public String getCommuName() {
		return name;
	}

	public long getId() {
		return id;
	}

	public String getDes() {
		return desc;
	}

}
